import java.awt.event.*;
import javax.swing.Timer;


public class TimedDescend implements ActionListener {
	public static int [][]board= new int[15][8];
	public static int[] variation={ 1, 0,0,  0,1, 0,2, 0,3, 0,4};
	public static int[]coordinates={0,0};
	
	public static void main(String[] args) {
        
		int delay = 1000;
        
		TimedDescend myTimedDescend = new TimedDescend();
        Timer myTimer = new Timer(delay, myTimedDescend);
        
		
		
		checkAndPlace();
		printBoard();
		
		myTimer.start();
		
		while(true) {
		}
	}

	
	
	//END MAIN END MAIN END MAIN MAIN END MAIN MAIN MAIN END MAIN MAIN MIAN END MIAN MIAN MNIA MNIA MAIN END NAIM NAIM NAIM NIAM IMNA IMAN END NMIA IMAMAN IMAWMAN ARGSIGN END OFLAGINO TRLL FCDTJPG END MAIN MAIN MAIN END MAIN MAIN MAIN MAIN MAIN MAIN
	
	
	
	
	

	public void actionPerformed(ActionEvent ae) {
		
		moveDown();
		printBoard();
    }
 
	
	
	
	//public void go(int delay, int count) {
    //    //this.count = count;  
    //    Timer myTimer = new Timer(delay, this);
    //    myTimer.start();   
    //}
	
	
	
	// place.java methods down below place.java methods down below place.java methods down below place.java methods down below place.java methods down below place.java methods down below place.java methods down below
	
	
	public static void checkAndPlace ()		//coordination method (quite useless... could be removed)
	 {
		 if (possible())
		 {
			 place();			
		}				
	 }	
	 
	 
	 
	public static boolean possible()				//checks if the pento can be placed with these coordinates
	 {
	 		if (checkOn())
	 		{
	 			if(checkFree())
	 			{
	 				return true;
	 			}
	 		}
	 	
	 	return false;
	 }
	
	public static boolean checkOn ()			//checks if the whole pento will be on the board	 (sub-method of "possible".... to avoid out of bounce errors)
{
	 for(int b=1; b<variation.length; b=b+2)
	 {
		 if (0>coordinates[1]+variation[b+1] || coordinates[1]+variation[b+1] >= board[0].length || 0>coordinates[0]+variation[b]||coordinates[0]+variation[b]>=board.length || 0>coordinates[1] || coordinates[1] >= board[0].length || 0>coordinates[0]||coordinates[0]>=board.length)
		 {
			 System.out.println("variation "+variation[0]+" =not on board");
			 return false;
		 }
	 }
	return true;
}

	
public static boolean checkFree ()			//checks if nothing will be overwritten (sub-method of "possible")
{
	for (int a=1; a<10; a=a+2)
	{			
			
		if(board[coordinates[0]+variation[a]] [coordinates[1]+variation[a+1]]!=0)
		{
					
		System.out.println("variation "+variation[0]+" =overwrites");
		 	for(int b2=0; b2<9; b2++)
		 	{
		 		System.out.print(variation[b2]);
			}
		 	return false;
		
		}
	}
	return true;
}
	
	
	
	
	public static void moveDown()			//moves the pento down (if possible)
	{
		remove();
		coordinates[0]=coordinates[0]+1;
		if (possible())
		{
			place();			
		}
		else
		{
			coordinates[0]=coordinates[0]-1;	
			place();	
		}
	}
	
	public static void place ()			//places pento on board
{
	 board[coordinates[0]+variation[1]][coordinates[1]+variation[2]]=variation[0];
	 board[coordinates[0]+variation[3]][coordinates[1]+variation[4]]=variation[0];
	 board[coordinates[0]+variation[5]][coordinates[1]+variation[6]]=variation[0];
	 board[coordinates[0]+variation[7]][coordinates[1]+variation[8]]=variation[0];
	 board[coordinates[0]+variation[9]][coordinates[1]+variation[10]]=variation[0];
		 
}

public static void remove()			//removes the pento from the board
{
	 board[coordinates[0]+variation[1]][coordinates[1]+variation[2]]=0;
	 board[coordinates[0]+variation[3]][coordinates[1]+variation[4]]=0;
	 board[coordinates[0]+variation[5]][coordinates[1]+variation[6]]=0;
	 board[coordinates[0]+variation[7]][coordinates[1]+variation[8]]=0;
	 board[coordinates[0]+variation[9]][coordinates[1]+variation[10]]=0;
}


public static void printBoard()			//prints the board
{
	System.out.println();
	 for (int a=0; a<board.length; a++)
	 {
		for(int b=0; b<board[0].length; b++)
		{
			System.out.print(board[a][b]);
		}
		
		System.out.println(); 			
	}
	 System.out.println();
}
	
	
	
	
	
	
	
	
}	